package com.example.testapplication;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.testapplication.util.ViewUtil;

public class LoginShareActivity extends AppCompatActivity implements View.OnClickListener {
    private RadioGroup rg_login;
    private RadioButton rb_password;
    private RadioButton rb_verifycode;
    private EditText et_phone;
    private TextView tv_password;
    private EditText et_password;
    private Button btn_forget;
    private CheckBox ck_remember;

    private int mRequestCode = 0; // 跳转页面时的请求代码
    private int mType = 0; // 用户类型
    private boolean bRemember = false; // 是否记住密码
    private String mPassword = "111111"; //  默认密码
    private String mVerifyCode; // 验证码
    private SharedPreferences mShared; // 声明一个共享参数对象
    private String[] typeArray = {"个人用户", "公司用户"};



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_share);
        rg_login = findViewById(R.id.rg_loginI);
        rb_password = findViewById(R.id.rb_passwordI);
        rb_verifycode = findViewById(R.id.rb_verifycodeI);
        et_phone = findViewById(R.id.et_phoneI);
        tv_password = findViewById(R.id.tv_passwordI);
        et_password = findViewById(R.id.et_passwordI);
        btn_forget = findViewById(R.id.btn_forgetI);
        ck_remember = findViewById(R.id.ck_rememberI);
        rg_login.setOnCheckedChangeListener(new RadioListener());
        ck_remember.setOnCheckedChangeListener(new CheckListener());
        et_phone.addTextChangedListener(new HideTextWatcher(et_phone));
        et_password.addTextChangedListener(new HideTextWatcher(et_password));
        btn_forget.setOnClickListener(this);
        findViewById(R.id.btn_loginI).setOnClickListener(this);
        initTypeSpinner();
        // 从share_login.xml 中获取共享参数对象
        mShared = getSharedPreferences("share_login",MODE_PRIVATE);
        // 获取共享参数保存手机号码
        String phone = mShared.getString("phone","");
        String password = mShared.getString("password","");
        et_phone.setText(phone);
        et_password.setText(password);

    }
    // 初始化用户类型的下拉框
    private void initTypeSpinner() {
        ArrayAdapter<String> typeAdapter = new ArrayAdapter<String>(this,R.layout.item_select,typeArray);
        typeAdapter.setDropDownViewResource(R.layout.item_dropdown);
        Spinner sp_type = findViewById(R.id.sp_type);
        sp_type.setPrompt("请选择用户类型");
        sp_type.setAdapter(typeAdapter);
        sp_type.setSelection(mType);
        sp_type.setOnItemSelectedListener(new TypeSelectedListener());
    }

    @Override
    public void onClick(View view) {
        String phone = et_phone.getText().toString();
        if(view.getId() == R.id.btn_forgetI){ // 点击 忘记密码
            if(phone.length() <11){
                Toast.makeText(this, "请输入正确的手机号码", Toast.LENGTH_SHORT).show();
                return;
            }
            if(rb_password.isChecked()){// 选择了密码方式校验，此时要跳到找回密码页面
                Intent intent = new Intent(this,LoginForgetActivity.class);
                // 携带手机号码跳转到找回密码页面
                intent.putExtra("phone",phone);
                startActivityForResult(intent,mRequestCode);

            }else if(rb_verifycode.isChecked()){ // 选择了验证码校验,此时生成六位随意数
                // 生成六位随机数字的验证码
                mVerifyCode = String.format("%06d",(int)(Math.random()*1000000 % 1000000));
                // 弹出提醒对话框,提示用户六位验证码数字
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("请记住验证码");
                builder.setMessage("手机号" + phone +",本次验证码是" + mVerifyCode + ",请输入验证码");
                builder.setPositiveButton("好的",null);
                AlertDialog alert = builder.create();
                alert.show();

            }

        }else if(view.getId() == R.id.btn_loginI){ // 点击了 登录
            if(phone.length() <11){
                Toast.makeText(this, "请输入正确手机号", Toast.LENGTH_SHORT).show();
                return;
            }
            if(rb_password.isChecked()){ // 密码方式校验
                if(!et_password.getText().toString().equals(mPassword)){
                    Toast.makeText(this, "请输入正确密码", Toast.LENGTH_SHORT).show();

                }else{ // 密码校验通过
                    loginSuccess(); // 提示用户登录成功

                }

            }else if(rb_verifycode.isChecked()){ // 验证码方式校验
                if(!et_password.getText().toString().equals(mVerifyCode)){
                    Toast.makeText(this, "请输入正确的验证码", Toast.LENGTH_SHORT).show();
                }else{
                    loginSuccess();
                }

            }

        }

    }

    private void loginSuccess() {
        String desc = String.format("您的手机号码是%s，类型是%s。恭喜你通过登录验证，点击“确定”按钮返回上个页面",
                et_phone.getText().toString(), typeArray[mType]);
        // 弹出提醒对话框，提示用户登录成功
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("登录成功");
        builder.setMessage(desc);
        builder.setPositiveButton("确定返回", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                finish();
            }
        });

        builder.setNegativeButton("我再看看",null);
        AlertDialog alert = builder.create();
        alert.show();

        // 如果勾选了 记住密码 就把手机号码和密码都保存到共存参数中
        if(bRemember){
            SharedPreferences.Editor editor = mShared.edit(); // 获取编辑器的对象
            editor.putString("phone",et_phone.getText().toString()); // 添加名叫phone的手机号码
            editor.putString("password",et_password.getText().toString());
            editor.commit();
        }
    }

    // 定义登录方式的单选监听器
    private class RadioListener implements RadioGroup.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            if(i == R.id.rb_passwordI){
                tv_password.setText("登录密码：");
                et_password.setHint("请输入密码");
                btn_forget.setText("忘记密码");
                ck_remember.setVisibility(View.VISIBLE);
            }else if(i == R.id.rb_verifycodeI){
                tv_password.setText("　验证码：");
                et_password.setHint("请输入验证码");
                btn_forget.setText("获取验证码");
                ck_remember.setVisibility(View.INVISIBLE);
            }

        }
    }
    // 定义是否记住密码的勾选监听器
    private class CheckListener implements CompoundButton.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if(compoundButton.getId() == R.id.ck_rememberI){
                bRemember = b;
            }

        }
    }
    // 定义编辑框的文本变化监听器
    private class HideTextWatcher implements TextWatcher {
        private EditText mView;
        private int mMaxLength;
        private CharSequence mStr; // ??
        public HideTextWatcher(EditText v) {
            super();
            mView = v;
            mMaxLength = ViewUtil.getMaxLength(v);
        }

        // 编辑框的输入文本变化前触发
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }
        // 在编辑框输入文本变化时触发
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            mStr = charSequence;

        }
        // 在编辑框的输入文本变化后触发
        @Override
        public void afterTextChanged(Editable editable) {
            if(TextUtils.isEmpty(mStr)){
                return;
            }
            // 手机号码输入达到11位 或者密码/验证输入达到6位，都关闭输入法软键盘
            if((mStr.length() ==11 && mMaxLength == 11) || (mStr.length()==6 && mMaxLength ==6)){
                ViewUtil.hideAllInputMethod(LoginShareActivity.this,mView);
            }

        }
    }
    // 定义用户类型的选择监听器
    private class TypeSelectedListener implements android.widget.AdapterView.OnItemSelectedListener {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            mType = i;

        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    }
}